var MongoClient = require('mongodb').MongoClient;
var Promise = require('bluebird');
var _ = require('lodash');

var settings = require('../settings')

var url = 'mongodb://'+ settings.host +':' + settings.port + '/' + settings.db + '/';
var db = null;

module.exports = {
  saveData: saveData,
  removeData: removeData,
  findData: findData,
  findOneData: findOneData,
  updateData: updateData,
  countData: countData,
  aggregateDate: aggregateDate
};

connectMongodb();

function connectMongodb() {
  if(!db){
    MongoClient.connect(url, function( err, _db ){
      if(err){
        console.log("DB connect error! " + err);
        return;
      }
      console.log('DB connect success!');
      db = _db;
    });
  }
}

function saveData(table, data) {
  console.log('save ' + table + ' ...');
  return new Promise(function (resolve, reject) {
    if(db) {
      db.collection(table).insert(data, {safe: true}, function (err, _data) {
        if (err) {
          reject(err);
        } else {
          resolve(_data[0]);
        }
      });
    } else {
      reject('DB is null !')
    }
  });
}

function removeData(table, query) {
  console.log('remove ' + table + ' ...');
  return new Promise(function (resolve, reject) {
    if(db) {
      db.collection(table).remove(query, function (err) {
        if (err) {
          reject(err);
        } else {
          resolve();
        }
      });
    } else {
      reject('DB is null !')
    }
  });
}

function findData(table, query, fields, options) {
  console.log('findDate ' + table + ' ...');
  return new Promise(function (resolve, reject) {
    if(db) {
      db.collection(table)
        .find(query, fields, options)
        .toArray(function (err, data) {
          if (err) {
            reject(err);
          } else {
            resolve(data);
          }
        });
    } else {
      reject('DB is null !')
    }
  });
}

function findOneData(table, query, fields) {
  console.log('findOneDate ' + table + ' ...');
  return new Promise(function (resolve, reject) {
    if(db) {
      db.collection(table).findOne(query, fields, function (err, data) {
        if (err) {
          reject(err);
        } else {
          resolve(data);
        }
      });
    } else {
      reject('DB is null !')
    }
  });
}

function updateData(table, query, newData) {
  console.log('updateData ' + table + ' ...');
  return new Promise(function (resolve, reject) {
    if(db) {
      db.collection(table).update(query, {$set:newData}, function (err, data) {
        if (err) {
          reject(err);
        } else {
          resolve(query);
        }
      });
    } else {
      reject('DB is null !')
    }
  });
}

function countData(table) {
  console.log('countData ' + table + ' ...');
  return new Promise(function (resolve, reject) {
    if(db) {
      db.collection(table).count(function (err, data) {
        if (err) {
          reject(err);
        } else {
          resolve(data);
        }
      });
    } else {
      reject('DB is null !')
    }
  });
}

function aggregateDate(table, query) {
  console.log('aggregateDate ' + table + ' ...');
  return new Promise(function (resolve, reject) {
    if(db) {
      db.collection(table).aggregate(query, function (err, data) {
        if (err) {
          reject(err);
        } else {
          resolve(data);
        }
      });
    } else {
      reject('DB is null !')
    }
  });
}


